﻿using HotelManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelManager.Service
{
    public class RoomService : IRoomService
    {
        private readonly HttpRestClient client;
        public RoomService(HttpRestClient client)
        {
            this.client = client;
        }
        public RoomDetailInfoData GetAll(int roomTypeId, int roomStateId, int pageIndex, int pageSize)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Get;
            StringBuilder sb = new StringBuilder();
            if (pageIndex == 0 || pageSize == 0)
            {
                sb.Append("&pageIndex=").Append(1).Append("&pageSize=").Append(10);
            }
            else
            {
                sb.Append("&pageIndex=").Append(pageIndex).Append("&pageSize=").Append(pageSize);
            }

            request.Route = $"api/Room/List?roomTypeId={roomTypeId}&roomStateId={roomStateId}" + sb.ToString();
            return client.ExecuteGetSync<RoomDetailInfoData>(request);
        }

        public async Task<RoomData> GetFirstOrDefaultAsync(int roomId)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Get;
            request.Route = $"api/Room/GetOne?roomId={roomId}";
            return await client.ExecuteAsync<RoomData>(request);
        }
        public async Task<ResultData<string>> DeleteAsync(RoomData roomData)
        {
            BaseRequest request = new BaseRequest();
            RoomIdData roomIdData = new RoomIdData() { roomId = roomData.roomId };
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Room/Delete";
            request.Parameter = roomIdData;
            return await client.ExecutePostAsync<ResultData<string>>(request);
        }

        public async Task<ResultData<string>> AddAsync(RoomData roomData)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Room/Add";
            request.Parameter = roomData;
            return await client.ExecutePostAsync<ResultData<string>>(request);
        }

        public ResultData<string> Update(RoomUpdateData roomUpdateData)
        {
            BaseRequest request = new BaseRequest();
            request.Method = RestSharp.Method.Post;
            request.Route = $"api/Room/Update";
            request.Parameter = roomUpdateData;
            return client.ExecuteSync<ResultData<string>>(request);
        }
    }
}
